﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace JQueryWidget.WidgetFrame.Modules
{
    public partial class BlankWidgetWrapper : System.Web.UI.UserControl, JQueryWidget.WidgetFrame.Common.IWidgetWrapper
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void LoadWidget(System.Xml.Linq.XElement widgetData)
        {
            Func<XElement, string, string> getElementValue = (ele, childEleName) => ele.Element(childEleName) != null ? ele.Element(childEleName).Value : null;
            Func<XElement, string, string> getAttributeValue = (ele, attributeName) => ele.Attribute(attributeName) != null ? ele.Attribute(attributeName).Value : null;

            var widgetDataObj = new
            {
                id = getAttributeValue(widgetData, "id"),
                title = getElementValue(widgetData, "title")
            };

            var widgetObj = (from wd in XDocument.Load(Server.MapPath("~/Data/widgets.xml")).Element("widgets").Elements("widget")
                             where wd.Element("id").Value == widgetDataObj.id
                             select new
                             {
                                 id = getElementValue(wd, "id"),
                                 title = getElementValue(wd, "title"),
                                 type = getElementValue(wd, "type"),
                                 src = getElementValue(wd, "src")
                             }).FirstOrDefault();
            if (widgetObj != null)
            {
                var widget = Page.LoadControl(widgetObj.src);
                Controls.Add(widget);
            }
        }


        public void LoadWidget(string templateDataId, string columnid, int index)
        {
            var templateData = (from td in XDocument.Load(Server.MapPath("~/Data/templatedata.xml")).Element("templatedatas").Elements("templatedata")
                                where td.Element("id").Value == templateDataId
                                select td
                                  ).FirstOrDefault();
            if (templateData != null)
            {
                var columnData = (from cd in templateData.Element("columns").Elements("column")
                                  where cd.Attribute("id").Value == columnid
                                  select cd).FirstOrDefault();
                if (columnData != null)
                {
                    var widgetData = (from wd in columnData.Element("widgets").Elements("widget")
                                      select wd).ToList().ElementAtOrDefault(index);
                    if (widgetData != null)
                    {
                        LoadWidget(widgetData);
                    }
                }
            }
        }


        public void LoadWidget(string widgetTypeId)
        {
            var widgetObj = (from wd in XDocument.Load(Server.MapPath("~/Data/widgets.xml")).Element("widgets").Elements("widget")
                             where wd.Element("id").Value == widgetTypeId
                             select new
                             {
                                 id = wd.Element("id").Value,
                                 title = wd.Element("title").Value,
                                 type = wd.Element("type").Value,
                                 src = wd.Element("src").Value
                             }).FirstOrDefault();
            var widget = Page.LoadControl(widgetObj.src);
            Controls.Add(widget);
        }
    }
}